package com.logistics.sms;

import com.alibaba.fastjson.JSON;
import com.logistics.util.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

@Component
public class SMSQueueConsumer {

    @RabbitListener(queues = "mq.sms")
    public void receive(String smsMap){

        //从rabbit取信息
        AtomicReference<String> mobile = new AtomicReference<>("");
        AtomicReference<String> code = new AtomicReference<>("");

        //包含手机号和验证码
        Map<String,String> map = JSON.parseObject(smsMap,Map.class);

        map.forEach((k,v) -> {
            mobile.set(k);
            code.set(v);
        });

        //发送短信
        SmsUtil.sendCode(mobile.toString(),code.toString());
        System.out.println("短信发送成功。");
    }
}
